//
//  DataInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 31/05/13.
//
//

#import "DataInterventoViewController.h"
#import "Util.h"
#import "DateUtil.h"


@interface DataInterventoViewController ()

@end

@implementation DataInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.array = [NSMutableArray arrayWithCapacity:0];
    [self.array addObject:@"Data"];
    [self.array addObject:@"Ora"];
    
    self.datePicker.date = self.intervento.dataora;
    [self.datePicker addTarget:self action:@selector(dateChanged:)
     forControlEvents:UIControlEventValueChanged];
    
    self.oldDate = [self.intervento.dataora copy];
    
    [self.table reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction) indietro{
	[[self navigationController] popViewControllerAnimated:YES];
}




- (void) dateChanged:(id)sender{
    self.intervento.dataora = self.datePicker.date;
    [self.table reloadData];
}

// TABELLA
#pragma mark - Tabella

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if([self.intervento.idintervento intValue]<0)return @"Data Nuovo Intervento";
	return [NSString stringWithFormat: @"Data Intervento %@",self.intervento.idintervento];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1]];
    
    NSString *name = [self.array objectAtIndex:indexPath.row];
    if([name isEqual:@"Data"]){
        cell.textLabel.text = name;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd/MM/yyyy"];
        NSString *dateString = [dateFormatter stringFromDate:self.intervento.dataora];
        cell.detailTextLabel.text = dateString;
    }
    if([name isEqual:@"Ora"]){
        cell.textLabel.text = name;
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        NSString *dateString = [dateFormatter stringFromDate:self.intervento.dataora];
        cell.detailTextLabel.text = dateString;
    }
    
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    return cell;
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [self.array objectAtIndex:indexPath.row];
    self.nameSelect = name;
    
    
    [self.table reloadData];
}

-(IBAction) ripristina{
    self.intervento.dataora = self.oldDate;
    self.datePicker.date = self.intervento.dataora;
    [self.table reloadData];
}

-(IBAction) adesso{
    self.intervento.dataora = [[NSDate alloc] init];
    self.datePicker.date = self.intervento.dataora;
    [self.table reloadData];
}

@end
